<?php
class recommendModel extends baseModel{
    protected $table = 'recommend';

    //推荐位列表
    public function recommend_list() {
        return $this->select('','','sequence asc,id asc');
    }

    //推荐位信息
    public function info($id) {
        return $this->find('id='.$id);
    }

    //获取指定推荐位列表
    public function content_list($id) {
        return $this->select('id in('.$id.')','','sequence asc,id asc');
    }

    //添加推荐位
    public function add($data)
    {

        $data['sequence']=intval($data['sequence']);
        return $this->insert($data);
    }
    //编辑推荐位
    public function edit($data)
    {
        $condition['id']=intval($data['id']);
        $data['sequence']=intval($data['sequence']);
        return $this->update($condition,$data); 
    }
    //删除推荐位
    public function del($id)
    {
        //删除关联
        model('admin_relation')->relation_del($id);
        ApiHook('hook_admin_recommend_relation_del_data',$id);
        return $this->delete('id='.intval($id));
    }

    //内容信息头部html
    public function recommend_info_html($info=array())
    {
        $recommend_list=$this->recommend_list();
        $html='';
        if(!empty($recommend_list)){
            if(!empty($info)){
                $recommend_array=model('recommend_relation')->relation_array($info['aid']);
            }
            $html.='<div class="formitm">
            <label class="lab" >推荐位：</label>
            <div class="ipt"> ';
            foreach ($recommend_list as $value) {
                    $html.='<input name="recommend[]" type="checkbox" value="'.$value['id'].'" ';
                    if(is_array($recommend_array)){ if(in_array($value['id'],$recommend_array)){
                        $html.=' checked="checked" ';
                    }}
                    $html.=' /> '.$value['name'].' &nbsp;&nbsp; ';
            }
            $html.='</div>
            </div>';
        }
        return $html;
    }

    public function label_recommend_list_sql($data=array())
    {
        if (!empty($data['recommend'])) {
            $where = " AND R.pid in(".in($data['recommend']).') ';
            $sql=" LEFT JOIN {$this->model->pre}recommend_relation R ON R.aid = A.aid LEFT JOIN {$this->model->pre}recommend P ON P.id = R.pid ";
        }
        return array('sql'=>$sql,'where'=>$where);
    }

    public function admin_content_list_tool_html()
    {
        $recommend_list=$this->recommend_list();
        $html='&nbsp;&nbsp;
        推荐位：
        <select id="recommend" name="recommend" onChange="ajaxload()">
            <option value="0">默认</option>';
            if(!empty($recommend_list)){
                foreach ($recommend_list as $value) {
                    $html.='<option value="'.$value['id'].'"';
                    if($_GET['recommend']==$value['id']){
                        $html.=' selected="selected" ';
                    }
                    $html.=' > '.$value['name'].'</option>';
                }
            }
        $html.='
        </select>
        ';
        return $html;
    }

    public function admin_content_list_sql($data=array())
    {
        //推荐位
        $recommend=intval($_POST['recommend']);
        if(!empty($recommend)){
            $where=' AND R.pid='.$recommend;
             $sql=" LEFT JOIN {$this->model->pre}recommend_relation R ON R.aid = A.aid ";
        }
        return array('where'=>$where,'sql'=>$sql );



    }

	
}